-
Notifications
You must be signed in to change notification settings - Fork 275
/
index.ts
77 lines (63 loc) · 2.83 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import * as restify from 'restify';
import { BotFrameworkAdapter, MemoryStorage, ConversationState, UserState } from 'botbuilder';
import { AdaptiveDialog, OnUnknownIntent, SendActivity, TextInput, IfCondition, RegExpRecognizer, OnIntent, EndTurn, BeginDialog, EndDialog } from 'botbuilder-dialogs-adaptive';
import { DialogManager } from 'botbuilder-dialogs';
// Create adapter.
// See https://aka.ms/about-bot-adapter to learn more about .bot file its use and bot configuration.
const adapter = new BotFrameworkAdapter({
appId: process.env.microsoftAppID,
appPassword: process.env.microsoftAppPassword,
});
// Create HTTP server.
const server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 3978, () => {
console.log(`\n${server.name} listening to ${server.url}`);
console.log(`\nGet Bot Framework Emulator: https://aka.ms/botframework-emulator`);
console.log(`\nTo talk to your bot, open echobot.bot file in the Emulator.`);
});
// Create bots DialogManager and bind to state storage
const bot = new DialogManager();
bot.conversationState = new ConversationState(new MemoryStorage());
bot.userState = new UserState(new MemoryStorage());
// Listen for incoming activities.
server.post('/api/messages', (req, res) => {
adapter.processActivity(req, res, async (context) => {
// Route activity to bot.
await bot.onTurn(context);
});
});
// Initialize bots root dialog
const dialogs = new AdaptiveDialog();
bot.rootDialog = dialogs;
//=================================================================================================
// Rules
//=================================================================================================
dialogs.recognizer = new RegExpRecognizer().addIntent('JokeIntent', /tell .*joke/i);
// Tell the user a joke
dialogs.addRule(new OnIntent('#JokeIntent', [], [
new BeginDialog('TellJokeDialog')
]));
// Handle unknown intents
dialogs.addRule(new OnUnknownIntent([
new BeginDialog('AskNameDialog')
]));
//=================================================================================================
// Child Dialogs
//=================================================================================================
const askNameDialog = new AdaptiveDialog('AskNameDialog', [
new IfCondition('user.name == null', [
new TextInput('user.name', `Hi! what's your name?`)
]),
new SendActivity(`Hi {user.name}. It's nice to meet you.`),
new EndDialog()
]);
dialogs.actions.push(askNameDialog);
const tellJokeDialog = new AdaptiveDialog('TellJokeDialog',[
new SendActivity(`Why did the 🐔 cross the 🛣️?`),
new EndTurn(),
new SendActivity(`To get to the other side...`),
new EndDialog()
]);
dialogs.actions.push(tellJokeDialog);